<?php
class BrandModel extends BaseModel
{
	private $_type = 'brand';

	public function getList($map = null, $limit = '0,10', $order = 'att_count DESC')
	{
		return $this->getListByWhere($map, 'id, name, ename, initials, image', $limit, $order);
	}

	public function getOne($id)
	{
		return $this->getById($id, 'id, name, ename, initials, intr, image');
	}

	public function getOneLine($id)
	{
		return $this->getById($id, 'id, name, ename, initials, image');
	}

	/**
	 * 搜索根据ename 获取品牌信息
	 * @param $name
	 * @return bool
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getOneByEname($name, $fields = 'id, name, ename, initials, intr')
	{
		$map = array();
		$map['ename'] = $name;
		$brand = $this->field($fields)->where($map)->find();
		return $brand;
	}

	/**
	 * 获取品牌图片地址
	 * @param int $id
	 * @return String 图片地址
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getBrandImage($id)
	{
		return $this->getManagedUrl($id, $this->_type);
	}

	public function getByIdDetailAndImage($id)
	{
		$image_url = null;
		$brand = $this->getById($id, 'image');
		if ($brand) {
			$image_url = $this->getBrandImage($brand['image']);
			$brand['image_url'] = $image_url;
		}
		return $brand;
	}

	/**
	 * 根据条件获取品牌首字母
	 * @param null $map
	 * @return mixed
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getInitials($map = null)
	{
		return $this->where($map)->field('initials')->order('initials')->group('initials')->select();
	}

	/**
	 * 获取推荐品牌
	 * @param $pid
	 * @param int $count
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getSimilar($pid, $count = 6)
	{
		$map = array();
		$map['pid'] = $pid;
		//获取品牌 获取图片 获取地址
		$brands = $this->field('id, name, small_image ')
		->where($map)
		->limit('0,' . $count)
		->order('rand()')
		->select();
		if ($brands) {
			foreach ($brands as $key => $brand) {
				$brands[$key]['image_url'] = $this->getBrandImage($brand['small_image']);
				$brands[$key]['ad_url'] = $this->getAdUrl(0, 0, $brand['id']);
			}
		}
		return $brands;
	}

	/**
	 * 检查品牌关注数量是否正确
	 * @param int $bid
	 * @param int $newCount
	 * @param int $attCount
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function attCheckCount($bid, $newCount, $attCount = 0){
		$isEdit = false;
		if(empty($attCount)){
			$isEdit = $attCount != $newCount ? true : false;
		}else{
			//获取品牌关注数量
			$brand = $this->field('att_count')->find($bid);
			$isEdit = $brand['att_count'] != $newCount ? true : false;
		}
		if($isEdit){
			//更新品牌关注数量
			$data = array();
			$data['att_count'] = $newCount;
			$this->where('id='.$bid)->save($data);
		}
	}
}

?>